home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 295_01 / bgethf.c < prev    next >
Text File  |  1989-12-28  |  3KB  |  105 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bgethf.c    1.2 - 89/10/31" */
  5.  
  6. #include <errno.h>
  7. /*#include <stddef.h>*/
  8. /*#include <string.h>*/
  9. #include "blkio_.h"
  10.  
  11. /*man---------------------------------------------------------------------------
  12. NAME
  13.      bgethf - get a header field from a block file
  14.  
  15. SYNOPSIS
  16.      #include <blkio.h>
  17.  
  18.      int bgethf(bp, offset, buf, bufsize)
  19.      BLKFILE *bp;
  20.      size_t offset;
  21.      void *buf;
  22.      size_t bufsize;
  23.  
  24. DESCRIPTION
  25.      The bgethf function reads a field from the header in the block
  26.      file associated with BLKFILE pointer bp into buf.  The field
  27.      begins offset characters from the beginning of the header and is
  28.      bufsize characters long.  buf must point to a storage area at
  29.      least bufsize characters long.
  30.  
  31.      bgethf will fail if one or more of the following is true:
  32.  
  33.      [EINVAL]       bp is not a valid BLKFILE pointer.
  34.      [EINVAL]       bn or bufsize is less than 1.
  35.      [EINVAL]       buf is the NULL pointer.
  36.      [EINVAL]       offset + bufsize extends beyond the
  37.                     boundary of the header.
  38.      [BEEOF]        bp is empty.
  39.      [BEEOF]        End of file encountered within header.
  40.      [BENOPEN]      bp is not open for reading.
  41.  
  42. SEE ALSO
  43.      bgeth, bgetbf, bputhf.
  44.  
  45. DIAGNOSTICS
  46.      Upon successful completion, a value of 0 is returned.  Otherwise,
  47.      a value of -1 is returned, and errno set to indicate the error.
  48.  
  49. ------------------------------------------------------------------------------*/
  50. int bgethf(bp, offset, buf, bufsize)
  51. BLKFILE *bp;
  52. size_t offset;
  53. void *buf;
  54. size_t bufsize;
  55. {
  56.     /* validate arguments */
  57.     if (!b_valid(bp) || (buf == NULL) || (bufsize < 1)) {
  58.         errno = EINVAL;
  59.         return -1;
  60.     }
  61.  
  62.     /* check if not open for reading */
  63.     if (!(bp->flags & BIOREAD)) {
  64.         errno = BENOPEN;
  65.         return -1;
  66.     }
  67.  
  68.     /* check if block boundary is crossed */
  69.     if ((offset + bufsize) > bp->hdrsize) {
  70.         errno = EINVAL;
  71.         return -1;
  72.     }
  73.  
  74.     /* check if header not yet written */
  75.     if (bp->endblk < 1) {
  76.         errno = BEEOF;
  77.         return -1;
  78.     }
  79.  
  80.     /* check if not buffered */
  81.     if (bp->bufcnt == 0) {
  82.         if (b_ugetf(bp, (bpos_t)0, offset, buf, bufsize) == -1) {
  83.             BEPRINT;
  84.             return -1;
  85.         }
  86.         errno = 0;
  87.         return 0;
  88.     }
  89.  
  90.     /* check if buffer is not loaded */
  91.     if (!(b_block_p(bp, (size_t)0)->flags & BLKREAD)) {
  92.         b_block_p(bp, (size_t)0)->bn = 0; /* read header from file */
  93.         if (b_get(bp, (size_t)0) == -1) {
  94.             BEPRINT;
  95.             return -1;
  96.         }
  97.     }
  98.  
  99.     /* copy from block buffer into buf */
  100.     memcpy(buf, ((char *)b_blkbuf(bp, (size_t)0) + offset), bufsize);
  101.  
  102.     errno = 0;
  103.     return 0;
  104. }
  105.